Skip to content

London | 26-JUL-SDC |Ping Wang | Sprint 2 |Improve precomputing CYF-1183 - #233

Open
pathywang wants to merge 2 commits into
CodeYourFuture:mainfrom
pathywang:improve-precomputing
Open

pathywang wants to merge 2 commits into
CodeYourFuture:mainfrom
pathywang:improve-precomputing

Conversation

@pathywang

@pathywang pathywang commented Oct 4, 2026 •

Copy link
Copy Markdown

Self checklist

  • I have titled my PR with Region | Cohort | FirstName LastName | Sprint | Assignment Title
  • My changes meet the requirements of the task
  • I have tested my changes
  • My changes follow the style guide

Task code

CYF-1183

Changelist

I do not feel very struggle for these two tasks with google help but need more practice,I think

@github-actions

This comment has been minimized.

@pathywang pathywang changed the title London | 26-JUL-SDC |Ping Wang Spring 2 |Improve precomputing CYF-1183 London | 26-JUL-SDC |Ping Wang | Sprint 2 |Improve precomputing CYF-1183 Oct 4, 2026
@github-actions

This comment has been minimized.

@pathywang pathywang added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Oct 4, 2026
Comment on lines -11 to +22
for string_index, string in enumerate(strings):
for other_string in strings[string_index+1:]:
common = find_common_prefix(string, other_string)
if len(common) > len(longest):
longest = common

for i in range(len(strings) - 1):
common = find_common_prefix(strings[i], strings[i + 1])

if len(common) > len(longest):
longest = common

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use complexity to explain how the new implementation is better than the original implementation?

@pathywang pathywang Oct 10, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks of course. The original implementation compares every possible pair of strings, resulting in O(n²) pair comparisons. The updated implementation sorts the strings and compares only adjacent pairs, reducing the number of prefix comparisons to O(n). Sorting takes O(n log n) comparisons, so the overall time complexity is O(n log n + n × m), where n is the number of strings and m is the maximum string length. It also avoids repeatedly creating list slices, reducing unnecessary memory allocations. Is it OK?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If string length, $m$, is to be taken into consideration, how does $m$ affect the complexity in the original implementation and the complexity of sorting?

@pathywang pathywang Oct 10, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. The maximum string length, m, affects the cost of comparing strings because finding their common prefix can take O(m) time in the worst case. In the original implementation, there are O(n²) pairs to compare, so the overall time complexity is O(n² × m). In the updated implementation, sorting n strings takes O(n × m × log n) in the worst case because each string comparison can take O(m). Comparing adjacent strings afterwards takes O(n × m). Therefore, the overall worst-case time complexity of the updated implementation is O(n × m × log n), which is an improvement over O(n² × m). Both implementations depend on the string length m, but your new implementation reduces the number of string comparisons from quadratic growth to sorting-level growth. So I think the complexity of sorting is better compared with the original implementation

@cjyuan cjyuan added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Oct 10, 2026
@pathywang pathywang added Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. and removed Reviewed Volunteer to add when completing a review with trainee action still to take. labels Oct 10, 2026
@cjyuan cjyuan added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Oct 10, 2026
@pathywang pathywang added Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. and removed Reviewed Volunteer to add when completing a review with trainee action still to take. labels Oct 10, 2026
@cjyuan cjyuan added Complete Volunteer to add when work is complete and all review comments have been addressed. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Oct 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Complete Volunteer to add when work is complete and all review comments have been addressed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants